<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名称" prop="supplierShopName">
        <el-input
          v-model="queryParams.supplierShopName"
          placeholder="请输入供应商店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务类目" prop="supplierTypeCode">
        <el-input
          v-model="queryParams.supplierTypeCode"
          placeholder="请输入供应商服务类目编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类目名称" prop="supplierTypeName">
        <el-input
          v-model="queryParams.supplierTypeName"
          placeholder="请输入供应商服务类目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactName">
        <el-input
          v-model="queryParams.contactName"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input
          v-model="queryParams.contactPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态名称" prop="statusName">
        <el-input
          v-model="queryParams.statusName"
          placeholder="请输入状态名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:supplier:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:supplier:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:supplier:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:supplier:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="店铺名称" align="center" prop="supplierShopName" />
      <el-table-column label="服务类目" align="center" prop="supplierTypeName" />
      <el-table-column label="联系人" align="center" prop="contactName" />
      <el-table-column label="联系电话" align="center" prop="contactPhone" width="120" />
      <el-table-column label="省" align="center" prop="province" />
      <el-table-column label="市" align="center" prop="city" />
      <el-table-column label="县" align="center" prop="county" />
      <el-table-column label="状态" align="center" prop="statusName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:supplier:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:supplier:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改上门宴资源服务商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="店铺名称" prop="supplierShopName">
          <el-input v-model="form.supplierShopName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="服务类目" prop="supplierTypeCode">
          <el-select v-model="form.supplierTypeCode" placeholder="请选择服务类目">
            <el-option
              v-for="dict in dict.type.supplier_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系人" prop="contactName">
          <el-input v-model="form.contactName" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="店铺地址">
          <el-cascader
            size="large"
            :options="options"
            v-model="selectedOptions"
            @change="handleChange">
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="detailAddress">
          <el-input v-model="form.detailAddress" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="主营项目" prop="mainProject">
          <el-input v-model="form.mainProject" placeholder="请输入主营项目" />
        </el-form-item>
<!--        <el-form-item label="店铺招牌" prop="shopSign">-->
<!--          <el-input v-model="form.shopSign" placeholder="请输入店铺招牌" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="营业执照" prop="businessLicense">-->
<!--          <el-input v-model="form.businessLicense" placeholder="请输入营业执照" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSupplier, getSupplier, delSupplier, addSupplier, updateSupplier } from "@/api/banquet/banquetSupplier";
import { regionData, CodeToText,TextToCode} from 'element-china-area-data';

export default {
  dicts: ['supplier_type',],
  name: "Supplier",
  data() {
    return {
      options: regionData,
      selectedOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 上门宴资源服务商表格数据
      supplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierShopName: null,
        supplierTypeCode: null,
        supplierTypeName: null,
        contactName: null,
        contactPhone: null,
        province: null,
        city: null,
        county: null,
        detailAddress: null,
        mainProject: null,
        shopSign: null,
        businessLicense: null,
        createdTime: null,
        modifyTime: null,
        statusId: null,
        statusName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        supplierTypeCode: [
          { required: true, message: "供应商服务类目不能为空", trigger: "blur" }
        ],
        contactName: [
          { required: true, message: "供应商店铺名称不能为空", trigger: "blur" }
        ],
        contactPhone: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],

      },

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询上门宴资源服务商列表 */
    getList() {
      this.loading = true;
      listSupplier(this.queryParams).then(response => {
        this.supplierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleChange(value){
      this.supplierProvince = CodeToText[value[0]];
      this.supplierCity = CodeToText[value[1]];
      this.supplierCounty = CodeToText[value[2]];
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        supplierShopName: null,
        supplierTypeCode: null,
        supplierTypeName: null,
        contactName: null,
        contactPhone: null,
        province: null,
        city: null,
        county: null,
        detailAddress: null,
        mainProject: null,
        shopSign: null,
        businessLicense: null,
        createdTime: null,
        modifyTime: null,
        statusId: null,
        statusName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加上门宴资源服务商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSupplier(id).then(response => {
        const {province,city,county} = response.data;
        if(province && city && county){
          this.selectedOptions[0] = TextToCode[province].code;
          this.selectedOptions[1] = TextToCode[province][city].code;
          this.selectedOptions[2] = TextToCode[province][city][county].code;
        }
        console.log();
        this.form = response.data;

        this.open = true;
        this.title = "修改上门宴资源服务商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            let formData = {
              ...this.form,
              province:this.supplierProvince,
              city:this.supplierCity,
              county:this.supplierCounty
            }
            updateSupplier(formData).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            let formData = {
              ...this.form,
              province:this.supplierProvince,
              city:this.supplierCity,
              county:this.supplierCounty
            }
            addSupplier(formData).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除上门宴资源服务商编号为"' + ids + '"的数据项？').then(function() {
        return delSupplier(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/supplier/export', {
        ...this.queryParams
      }, `supplier_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
